package com.hsnn.medstgmini.pro.plan.controller;

import java.lang.reflect.Type;
import java.sql.Timestamp;
import java.util.Date;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;
import com.hsnn.medstgmini.Constants;
import com.hsnn.medstgmini.pro.plan.service.WsjPriceAdjustManager;
import com.hsnn.medstgmini.pro.project.model.BidProject;
import com.hsnn.medstgmini.pro.project.service.BidProjectManager;
import com.hsnn.medstgmini.sys.model.SysUser;
import com.hsnn.medstgmini.util.DateUtil;
import com.hsnn.medstgmini.util.Pagination;

/**
 * @category 卫生局调价管理
 * @author wangbing
 * @date 2015-11-12 17:34:40
 */
@Controller
@RequestMapping("/wsjPriceAdjust")
public class WsjPriceAdjustController {
	private static final Logger log = Logger.getLogger(WsjPriceAdjustController.class);
	
	@Autowired
	private WsjPriceAdjustManager wsjPriceAdjustManager;
	
	@Autowired
	private HttpSession session;
	
	@Autowired
	private HttpServletRequest request;
	
	@Autowired
	private BidProjectManager bidProjectManager;

	/**
	 * @category 获取当前登陆人信息
	 * @date 2015年6月14日
	 * @return
	 */
	private SysUser getUser() {
		return (SysUser) session.getAttribute(Constants.USERINFO);
	}
	
	/**
	 * 
	 *@category 获取参数打印
	 *@author wangbing
	 *@date 2015年6月9日
	 *Pagination
	 */
	@SuppressWarnings("rawtypes")
	private void printParams(){
		Enumeration paramNames = request.getParameterNames();
		try {
			while (paramNames.hasMoreElements()) {
				String paramName = (String) paramNames.nextElement();
				String paramValue = request.getParameter(paramName);
				log.info(paramName+":"+paramValue);
			}
		} catch (Exception e) {
			log.error("",e);
		}
		
	}
	
	/**
	 * @category 进入新增调价计划页面
	 * @auth wangbing
	 * @return
	 * @date 2015-11-13 08:32:31
	 */
	@RequestMapping("/toAddPriceAdjustPlan")
	public String toAddPriceAdjustPlan() {
		return "/pro/plan/addPriceAdjustPlan";
	}
	
	/**
	 * @category 调价计划新增
	 * @auth wangbing
	 * @return
	 * @date 2015-11-13 08:32:31
	 */
	@ResponseBody
	@RequestMapping("/priceAdjustPlanAdd")
	public Pagination priceAdjustPlanAdd(){
		printParams();
		Pagination page = new Pagination();
		String priceAdjPlanName = request.getParameter("priceAdjPlanName");
		String executeCat = request.getParameter("executeCat");
		String executeDatetime = request.getParameter("executeDatetime");
		String remark = request.getParameter("remark");
		String priceAdjAccord = request.getParameter("priceAdjAccord");
		if(StringUtils.isBlank(priceAdjPlanName)){
			page.setSuccess(false);
			page.setMsg("调价计划名称不能为空!");
			return page;
		}
		if(wsjPriceAdjustManager.validateRepeat(priceAdjPlanName.trim(), "")){
			page.setSuccess(false);
			page.setMsg("调价计划名称重复!");
			return page;
		}
		if(StringUtils.isBlank(executeCat)){
			page.setSuccess(false);
			page.setMsg("请选择执行类型!");
			return page;
		}
		if("2".equals(executeCat)){
			if(StringUtils.isBlank(executeDatetime)){
				page.setSuccess(false);
				page.setMsg("请选择执行时间!");
				return page;
			}
			if(DateUtil.parseDateHHTime(executeDatetime).getTime()<new Date().getTime()){
				page.setSuccess(false);
				page.setMsg("执行时间不能小于当前时间!");
				return page;
			}
			page.getConditions().put("executeDatetime", executeDatetime);
		}
		if(StringUtils.isBlank(priceAdjAccord)){
			page.setSuccess(false);
			page.setMsg("调价依据不能为空!");
			return page;
		}
		page.getConditions().put("priceAdjPlanName", priceAdjPlanName.trim());
		page.getConditions().put("executeCat", executeCat);
		page.getConditions().put("priceAdjAccord", priceAdjAccord.trim());
		page.getConditions().put("remark", remark.trim());
		page.getConditions().put("orgId", getUser().getOrgId());
		page.getConditions().put("userId", getUser().getId());
		page.getConditions().put("uuid", UUID.randomUUID().toString());
		try {
			wsjPriceAdjustManager.addPriceAdjPlan(page);
			page.setSuccess(true);
		} catch (Exception e) {
			page.setSuccess(false);
			page.setMsg("数据异常!");
			e.printStackTrace();
		}
		return page;
	}
	
	/**
	 * @category 进入未处理调价计划页面
	 * @auth wangbing
	 * @return
	 * @date 2015-11-13 08:32:31
	 */
	@RequestMapping("/toNoPriceAdjPlanList")
	public String toNoPriceAdjPlanList() {
		return "/pro/plan/noPriceAdjPlanList";
	}
	
	/**
	 * @category 获取未处理调价计划列表
	 * @auth wangbing
	 * @return
	 * @date 2015-11-13 08:32:31
	 */
	@ResponseBody
	@RequestMapping("/getNoPriceAdjPlanList")
	public Pagination getNoPriceAdjPlanList(){
		printParams();
		Pagination page = new Pagination(request);
		page.getConditions().put("priceAdjPlanName", request.getParameter("priceAdjPlanName"));
		page.getConditions().put("executeCat", request.getParameter("executeCat"));
		page.getConditions().put("createStartTime", request.getParameter("createStartTime"));
		page.getConditions().put("createEndTime", request.getParameter("createEndTime"));
		page.getConditions().put("executeStartTime", request.getParameter("executeStartTime"));
		page.getConditions().put("executeEndTime", request.getParameter("executeEndTime"));
		page.getConditions().put("orgId", getUser().getOrgId());
		try {
			wsjPriceAdjustManager.getNoPriceAdjPlanList(page);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return page;
	}
	
	/**
	 * @category 进入编辑调价计划页面
	 * @auth wangbing
	 * @return
	 * @date 2015-11-13 08:32:31
	 */
	@RequestMapping("/toEditPriceAdjPlan")
	public String toEditPriceAdjPlan() {
		printParams();
		String id = request.getParameter("id");
		try {
			Map<String,Object> plan = wsjPriceAdjustManager.getPriceAdjPlanById(id);
			request.setAttribute("plan", plan);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "/pro/plan/editPriceAdjPlan";
	}
	
	/**
	 * @category 调价计划编辑
	 * @auth wangbing
	 * @return
	 * @date 2015-11-13 08:32:31
	 */
	@ResponseBody
	@RequestMapping("/priceAdjustPlanEdit")
	public Pagination priceAdjustPlanEdit(){
		printParams();
		Pagination page = new Pagination();
		String priceAdjPlanName = request.getParameter("priceAdjPlanName");
		String executeCat = request.getParameter("executeCat");
		String executeDatetime = request.getParameter("executeDatetime");
		String remark = request.getParameter("remark");
		String priceAdjAccord = request.getParameter("priceAdjAccord");
		String priceAdjPlanId = request.getParameter("priceAdjPlanId");
		if(StringUtils.isBlank(priceAdjPlanName)){
			page.setSuccess(false);
			page.setMsg("调价计划名称不能为空!");
			return page;
		}
		if(wsjPriceAdjustManager.validateRepeat(priceAdjPlanName.trim(), priceAdjPlanId)){
			page.setSuccess(false);
			page.setMsg("调价计划名称重复!");
			return page;
		}
		if(StringUtils.isBlank(executeCat)){
			page.setSuccess(false);
			page.setMsg("请选择执行类型!");
			return page;
		}
		if("2".equals(executeCat)){
			if(StringUtils.isBlank(executeDatetime)){
				page.setSuccess(false);
				page.setMsg("请选择执行时间!");
				return page;
			}
			if(DateUtil.parseDateHHTime(executeDatetime).getTime()<new Date().getTime()){
				page.setSuccess(false);
				page.setMsg("执行时间不能小于当前时间!");
				return page;
			}
			page.getConditions().put("executeDatetime", executeDatetime);
		}
		if(StringUtils.isBlank(priceAdjAccord)){
			page.setSuccess(false);
			page.setMsg("调价依据不能为空!");
			return page;
		}
		page.getConditions().put("priceAdjPlanName", priceAdjPlanName.trim());
		page.getConditions().put("executeCat", executeCat);
		page.getConditions().put("priceAdjAccord", priceAdjAccord.trim());
		page.getConditions().put("remark", remark.trim());
		page.getConditions().put("userId", getUser().getId());
		page.getConditions().put("priceAdjPlanId",priceAdjPlanId);
		try {
			wsjPriceAdjustManager.editPriceAdjPlan(page);
			page.setSuccess(true);
		} catch (Exception e) {
			page.setSuccess(false);
			page.setMsg("数据异常!");
			e.printStackTrace();
		}
		return page;
	}
	
	/**
	 * @category 进入调价计划明细页面
	 * @auth wangbing
	 * @return
	 * @date 2015-11-13 08:32:31
	 */
	@RequestMapping("/toPriceAdjPlanDetail")
	public String toPriceAdjPlanDetail() {
		printParams();
		String id = request.getParameter("id");
		try {
			Map<String,Object> plan = wsjPriceAdjustManager.getPriceAdjPlanById(id);
			List<BidProject> projList = bidProjectManager.getBidProjectSelectList(null, null);
			request.setAttribute("projList", projList);
			request.setAttribute("plan", plan);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "/pro/plan/priceAdjPlanDetail";
	}
	
	/**
	 * @category 获取调价计划明细列表
	 * @auth wangbing
	 * @return
	 * @date 2015-11-13 08:32:31
	 */
	@ResponseBody
	@RequestMapping("/getPriceAdjPlanDetailList")
	public Pagination getPriceAdjPlanDetailList() {
		printParams();
		Pagination page = new Pagination(request);
		page.getConditions().put("projId", request.getParameter("projId"));
		page.getConditions().put("goodsCode", request.getParameter("goodsCode"));
		page.getConditions().put("drugName", request.getParameter("drugName"));
		page.getConditions().put("prodComp", request.getParameter("prodComp"));
		page.getConditions().put("drugCode", request.getParameter("drugCode"));
		page.getConditions().put("priceAdjPlanId", request.getParameter("priceAdjPlanId"));
		try {
			wsjPriceAdjustManager.getPriceAdjPlanDetailList(page);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return page;
	}
	
	/**
	 * @category 弹出添加药品页面
	 * @auth wangbing
	 * @return 药品调价操作页面
	 * @date 2015-11-15 13:44:24
	 */
	@RequestMapping("/toPriceAdjPlanAddDrug")
	public String toPriceAdjPlanAddDrug() {
		printParams();
		String id = request.getParameter("id");
		try {
			List<BidProject> projList = bidProjectManager.getBidProjectSelectList(null, null);
			Map<String,Object> pricePlan = wsjPriceAdjustManager.getPriceAdjPlanById(id);
			request.setAttribute("projList", projList);
			request.setAttribute("pricePlan", pricePlan);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "/pro/plan/priceAdjPlanAddDrug";
	}
	
	/**
	 * @category 获取未加入调价计划的商品列表json
	 * @auth wangbing
	 * @return 商品列表json
	 * @date 2015年6月15日
	 */
	@RequestMapping("/getPriceAdjPlanDurgList")
	@ResponseBody
	public Pagination getPriceAdjPlanDurgList() {
		printParams();
		Pagination page = new Pagination(request);
		page.getConditions().put("projName", request.getParameter("projName"));
		page.getConditions().put("goodsCode", request.getParameter("goodsCode"));
		page.getConditions().put("drugName", request.getParameter("drugName"));
		page.getConditions().put("prodComp", request.getParameter("prodComp"));
		page.getConditions().put("drugCode", request.getParameter("drugCode"));
		page.getConditions().put("priceid", request.getParameter("priceid"));
		page.getConditions().put("orgId", getUser().getOrgId());
		try {
			wsjPriceAdjustManager.getPriceAdjPlanDurgList(page);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return page;
	}
	
	/**
	 * 
	 * @category 加入调价计划
	 * @auth wangbing
	 * @return Pagination
	 * @date 2015年6月23日
	 */
	@RequestMapping("/addDetail")
	@ResponseBody
	public Pagination addDetail(){
		printParams();
		Pagination page = new Pagination();
		String drugs = request.getParameter("drugs");
		String addDatetime = DateUtil.dateTime(new Timestamp(System.currentTimeMillis()));
		try {
			if (drugs != null && !"".equals(drugs)) {
				Gson gson = new Gson();
				Type type = new TypeToken<List<Map<String,String>>>() {}.getType();
				List<Map<String,String>> list = gson.fromJson(drugs, type);
				for (Map<String,String> data : list) {
					data.put("addUser", getUser().getId());
					data.put("addDatetime",addDatetime);
					data.put("priceAdjPalnDetailId", UUID.randomUUID().toString());
				}
				page.setOperCount(wsjPriceAdjustManager.addDetail(list));
				page.setSuccess(true);
			}
		} catch (JsonSyntaxException e) {
			page.setSuccess(false);
			e.printStackTrace();
		}
		return page;
	}
	
	/**
	 * 
	 * @category 删除调价计划明细
	 * @auth wangbing
	 * @return Pagination
	 * @date 2015年6月23日
	 */
	@RequestMapping("/deleteDetail")
	@ResponseBody
	public Pagination deleteDetail(){
		printParams();
		Pagination page = new Pagination();
		String drugs = request.getParameter("drugs");
		try {
			if (drugs != null && !"".equals(drugs)) {
				Gson gson = new Gson();
				Type type = new TypeToken<List<String>>() {}.getType();
				List<String> list = gson.fromJson(drugs, type);
				page.setOperCount(wsjPriceAdjustManager.deleteDetail(list));
				page.setSuccess(true);
			}
		} catch (JsonSyntaxException e) {
			page.setSuccess(false);
			e.printStackTrace();
		}
		return page;
	}
	
	/**
	 * 
	 * @category 保存调价计划明细
	 * @auth wangbing
	 * @return Pagination
	 * @date 2015年6月23日
	 */
	@RequestMapping("/saveDetail")
	@ResponseBody
	public Pagination saveDetail(){
		printParams();
		Pagination page = new Pagination();
		String drugs = request.getParameter("drugs");
		try {
			if (drugs != null && !"".equals(drugs)) {
				Gson gson = new Gson();
				Type type = new TypeToken<List<Map<String,String>>>() {}.getType();
				List<Map<String,String>> list = gson.fromJson(drugs, type);
				page.setOperCount(wsjPriceAdjustManager.saveDetail(list));
				page.setSuccess(true);
			}
		} catch (JsonSyntaxException e) {
			page.setSuccess(false);
			e.printStackTrace();
		}
		return page;
	}
	
	/**
	 * @category 执行调价计划
	 * @auth wangbing
	 * @date 2015年6月15日
	 */
	@RequestMapping("/runPriceAdjPlan")
	@ResponseBody
	public Pagination runPriceAdjPlan() {
		printParams();
		Pagination page = new Pagination(request);
		String planId = request.getParameter("planId");
		page.getConditions().put("planId", planId);
		page.getConditions().put("userId", getUser().getId());
		try {
			wsjPriceAdjustManager.runPriceAdjPlan(page);
			page.setSuccess(true);
		} catch (Exception e) {
			page.setSuccess(false);
			log.error(e.getMessage(),e);
		} catch (Error error) {
			page.setSuccess(false);
			log.error(error.getMessage());
		}
		return page;
	}
	
	/**
	 * @category 进入已处理调价计划页面
	 * @auth wangbing
	 * @return
	 * @date 2015-11-13 08:32:31
	 */
	@RequestMapping("/toPriceAdjPlanList")
	public String toPriceAdjPlanList() {
		return "/pro/plan/priceAdjPlanList";
	}
	
	/**
	 * @category 获取已处理调价计划列表
	 * @auth wangbing
	 * @return
	 * @date 2015-11-13 08:32:31
	 */
	@ResponseBody
	@RequestMapping("/getPriceAdjPlanList")
	public Pagination getPriceAdjPlanList(){
		printParams();
		Pagination page = new Pagination(request);
		page.getConditions().put("priceAdjPlanName", request.getParameter("priceAdjPlanName"));
		page.getConditions().put("executeCat", request.getParameter("executeCat"));
		page.getConditions().put("createStartTime", request.getParameter("createStartTime"));
		page.getConditions().put("createEndTime", request.getParameter("createEndTime"));
		page.getConditions().put("executeStartTime", request.getParameter("executeStartTime"));
		page.getConditions().put("executeEndTime", request.getParameter("executeEndTime"));
		page.getConditions().put("orgId", getUser().getOrgId());
		try {
			wsjPriceAdjustManager.getPriceAdjPlanList(page);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return page;
	}
	
	/**
	 * @category 跳转已处理调价计划明细列表页面
	 * @author wangbing
	 * @date 2015-11-16 09:28:22
	 * @param @param 
	 * @param @return page
	 */
	@RequestMapping("/toPriceDetailList")
	public String toPriceDetailList() {
        List<BidProject> projList = bidProjectManager.getBidProjectSelectList(null, null);
		String priceAdjPlanId = request.getParameter("priceAdjPlanId");
		if (StringUtils.isNotEmpty(priceAdjPlanId)) {
			Map<String,Object> pricePlan = wsjPriceAdjustManager.getPriceAdjPlanById(priceAdjPlanId);
			request.setAttribute("projList", projList);
			request.setAttribute("plan", pricePlan);
			request.setAttribute("priceAdjPlanId", priceAdjPlanId);
		}
		return "/pro/plan/priceDetailList";
	}
	
	/**
	 * @category 获取已处理调价计划明细列表
	 * @author wangbing
	 * @date 2015-11-16 09:28:22
	 * @param @param 
	 * @param @return page
	 */
	@RequestMapping("/getPriceDetailList")
	@ResponseBody
	public Pagination getPriceDetailList() {
		Pagination page = new Pagination(request);
		page.getConditions().put("goodsCode", request.getParameter("goodsCode"));
		page.getConditions().put("drugCode", request.getParameter("drugCode"));
		page.getConditions().put("drugName", request.getParameter("drugName"));
		page.getConditions().put("prodComp", request.getParameter("compName"));
		page.getConditions().put("projId", request.getParameter("projName"));	
		page.getConditions().put("priceAdjPlanId", request.getParameter("priceAdjPlanId"));
		try {
			wsjPriceAdjustManager.getPriceDetailList(page);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return page;
	}
	
	/**
	 * @category 跳转商品历史价格查看
	 * @author wangbing
	 * @date 2015-11-16 09:28:22
	 * @param @param 
	 * @param @return page
	 */
	@RequestMapping("/toGoodsHistoryPriceList")
	public String toGoodsHistoryPriceList() {
        List<BidProject> projList = bidProjectManager.getBidProjectSelectList(null, null);
		request.setAttribute("projList", projList);
		return "/pro/plan/goodsHistoryPriceList";
	}
	
	/**
	 * @category 获取商品历史价格列表
	 * @author wangbing
	 * @date 2015-11-16 09:28:22
	 * @param @param 
	 * @param @return page
	 */
	@RequestMapping("/getGoodsHistoryPriceList")
	@ResponseBody
	public Pagination getGoodsHistoryPriceList() {
		Pagination page = new Pagination(request);
		page.getConditions().put("goodsCode", request.getParameter("goodsCode"));
		page.getConditions().put("drugCode", request.getParameter("drugCode"));
		page.getConditions().put("drugName", request.getParameter("drugName"));
		page.getConditions().put("prodComp", request.getParameter("prodComp"));
		page.getConditions().put("projId", request.getParameter("projName"));
		page.getConditions().put("startTime", request.getParameter("startTime"));
		page.getConditions().put("endTime", request.getParameter("endTime"));
		page.getConditions().put("projName", request.getParameter("projName"));
		page.getConditions().put("orgId", getUser().getOrgId());
		try {
			wsjPriceAdjustManager.getGoodsHistoryPriceList(page);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return page;
	}

}
